$(function() {
	//panel初始化
	$('#dRolePanel').panel({
		border:false
	});
	
	//菜单按钮初始化
	$('#addRoleBtn').linkbutton({plain:true,iconCls:'icon-role-add'});
	$('#editRoleBtn').linkbutton({plain:true,iconCls:'icon-role-edit'});
	$('#delRoleBtn').linkbutton({plain:true,iconCls:'icon-role-del'});
	$('#saveRoleBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#closeRoleBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	//$('#aShowRole').linkbutton({plain:true,iconCls:'icon-role-search'});
	
	//添加窗口
	$('#dRoleWindow').window({
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-app',
		width:245,
		height:130
	});
	
	//DataGrid初始化
	$('#dRoleDataGrid').datagrid({
		url:'roleAjax!loadRoleData',
		nowrap: false,
		border:false,
		striped: true,
		collapsible:true,
		sortName: 'id',
		singleSelect:true,
		sortOrder: 'desc',
		remoteSort: false,
		idField:'id',			
		pagination:true,
		rownumbers:true,
		columns:[[
		           {field:'name', title:'名称', width:300, editor:'text'},
		           {field:'code', title:'代码', width:150, editor:'text'},
		           {field:'des', title:'描述', width:300, editor:'text'}
				]]
	});
	
	//分页
	var pager = $('#dRoleDataGrid').datagrid('getPager');
	$(pager).pagination({
		pageSize: 15,
		pageList: [15, 30, 50, 100],
		beforePageText: '第',
		afterPageText: '页    共 {pages} 页',
		displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		onSelectPage:function(pageNumber, pageSize){
			$('#dRoleDataGrid').datagrid('options').url = 'roleAjax!loadRoleData?pageNo=' + pageNumber + '&pageSize='+ pageSize;
			$('#dRoleDataGrid').datagrid('reload', pageNumber);
		}
	});
});

//打开窗口
function openRoleWindow(type) {
	$('#roleName').val('');
	if(type == 'del'){
		removeRole();
		return;
	}else if(type == 'add'){
		roleAdd();
		return;
	}else if(type == 'edit'){
		roleEdit();
		return;
	}else{
		$.messager.alert('提示','非法操作!',"error");
		return;
	}
}

//增加角色
function roleAdd(){
	$('#dRoleDataGrid').datagrid('endEdit', lastIndex);
	$('#dRoleDataGrid').datagrid('appendRow',{
		id:'',
		name:'',
		code:'',
		des:''
	});	
	var lastIndex = $('#dRoleDataGrid').datagrid('getRows').length-1;
	$('#dRoleDataGrid').datagrid('selectRow', lastIndex);
	$('#dRoleDataGrid').datagrid('beginEdit', lastIndex);
}

//编辑角色
function roleEdit(){
	var row = $('#dRoleDataGrid').datagrid('getSelected');
	if (row) {
		if(row.id == 1){
			$.messager.alert('提示','系统默认角色不允许编辑!');
			return;
		}
		var rowIndex = $('#dRoleDataGrid').datagrid('getRowIndex', row);
		$('#dRoleDataGrid').datagrid('beginEdit', rowIndex);
	}
}

//保存角色
function roleSave(){
	var rows = $('#dRoleDataGrid').datagrid('getRows');
	for ( var i = 0; i < rows.length; i++) {
		$('#dRoleDataGrid').datagrid('endEdit', i);
	}
	
	if ($('#dRoleDataGrid').datagrid('getChanges').length) {
		var inserted = $('#dRoleDataGrid').datagrid('getChanges', "inserted");
		var updated = $('#dRoleDataGrid').datagrid('getChanges', "updated");
		var effectRow = new Object();
		
		if (inserted.length) {
			effectRow["inserted"] = JSON.stringify(inserted);
		}
		if (updated.length) {
			effectRow["updated"] = JSON.stringify(updated);
		}
		
		$.post("roleAjax!save", effectRow,
				function(data) {
						$('#dRoleDataGrid').datagrid('reload');
						$.messager.alert("提示", "保存成功!");					
		}, "json").error(function() {
			$('#dRoleDataGrid').datagrid('reload');
			$.messager.alert("提示", "保存失败!");
		});
		
	}
}

function removeRole(){
	var row = $('#dRoleDataGrid').datagrid('getSelected');	
	if(row==null){
		$.messager.alert('提示','请先选择一条记录!');
		return;
	}else{
		if(row.id == 1){
			$.messager.alert('提示','系统默认角色不允许删除!');
			return;
		}
		
		//判断角色下是否存在用户
		$.post('roleAjax!existUsersByRole', {id:row.id},
	        	function(data){
					if(data.result == "true"){
						$.messager.confirm('提示', '该角色下还存在用户, 是否强制删除?\r 角色删除后部分用户可能会登录失败!', function(r){
							if(r){
								delRole();
							}
						})
					}else{
						$.messager.confirm('提示', '确认要删除该角色?', function(r){
							if (r){
								delRole();
							}
						})
					}
				}
	        	, 'json');		
	}
}

function delRole(){
	var selectedRow = $('#dRoleDataGrid').datagrid('getSelected');
	$.post('roleAjax!delete', {id:selectedRow.id},
        	function(data){
				var index = $('#dRoleDataGrid').datagrid('getRowIndex', selectedRow);
				$('#dRoleDataGrid').datagrid('deleteRow', index);						
        	}
        	, 'json');
}
	
/*function showRole(){
	var row = $('#dRoleDataGrid').datagrid('getSelected');	
	var name = '权限管理';
	if(row==null){
		$.messager.alert('提示','请先选择一条记录!');
		return;
	}else{
		var tab = $('#centerTabs').tabs('getTab',name);
		if(tab == null){
			$('#centerTabs').tabs('add',{
				title:name,
				href:'authority!pl_List?roleId='+row.id,
				closable:true,
				cache:false
			});
		}else{
			$('#centerTabs').tabs('select',name);
			var tab = $('#centerTabs').tabs('getSelected');
			$('#centerTabs').tabs('update', {
				tab: tab,
				options:{
					title:name,
					href:'authority!pl_List?roleId='+row.id
				}
			});
			
		}
	}
}*/
